"""
Python Implementation of Listerner/Trigger Macnism, and EventManager.

LiangZi, 2021.06.07
"""
from rbac import RBAC
from queue import Queue,Empty
from threading import Thread, Timer
import time

class myEventManager(object):
    def __init__(self):
        self.__is_run=0
        self.__event_queue=Queue()
        self.__thread=Thread(target=self.__run)
        self.__event_listerner_dict={}

    def __run(self):
        self.is_run=1
        while True:
            try:
                print("开始处理新的数据....")
                event=self.__event_queue.get(block=True,timeout=5)
                self.__event_process(event)
            except Empty:
                print("没有数据，等待中......")
                pass
    
    def start(self):
        self.__is_run=1
        print("事件管理器已启动")
        self.__thread.start()

    def stop(self):
        self.__is_run=0
        self.__thread.join()


    def __event_process(self,event):
        if event.type_ in self.__event_listerner_dict.keys():
            for listerner in self.__event_listerner_dict[event.type_]:
                listerner(event)
        print("事件处理完成")

    def addEventListerner(self,type_,listerner):
        if type_ not in self.__event_listerner_dict.keys():
            self.__event_listerner_dict[type_]=[]
        self.__event_listerner_dict[type_].append(listerner)
        print("监听器添加成功")

    def removeEventListerner(self,type_,listerner):
        if type_ in self.__event_listerner_dict.keys():
            if len(self.__event_listerner_dict[type_])>1:
                self.__event_listerner_dict[type_].remove(listerner)
            else:
                del self.__event_listerner_dict[type_]


    def sendEvent(self,event):
        print("事件发送成功")
        self.__event_queue.put(event)

class Event:
    def __init__(self,type_=None,datadict=None):
        self.type_=type_
        self.datadict=datadict



# def test():
#     #---------------test function passage.
#     def add(x):
#         return x+1
#     class queryANumber(Event):
#         def __init__(self,type_,datadict):
#             super(queryANumber,self).__init__(type_,datadict)
#     myquery=queryANumber("query_plus1",{"input":6,"action":add})
#     def testlisterner(queryANumber):
#         print("sss")
#         x=queryANumber.datadict["action"](queryANumber.datadict["input"])
#         print(x)
#         return x

#     def newTestListerner(queryANumber):
#         print("lalala")
#         x=queryANumber.datadict["action"](queryANumber.datadict["input"])
#         print(x)
#         return x

#     # result=testlisterner(myquery)
#     # print(result)

#     #-------------test event manager.
#     myEventManager1=myEventManager()
#     myEventManager1.start()
#     myEventManager1.addEventListerner("query_plus1", testlisterner)
#     myEventManager1.sendEvent(myquery)
#     query2=queryANumber("query_plus1",{"input":10,"action":add})
#     myEventManager1.sendEvent(query2)
#     time.sleep(1.5)
#     myEventManager1.addEventListerner("query_plus1", newTestListerner)
#     time.sleep(1.5)
#     query3=queryANumber("query_plus1",{"input":13,"action":add})
#     myEventManager1.sendEvent(query3)
#     myEventManager1.stop()

# if __name__=="__main__":
#     test()
